package com.exsun.ex1077.ui.view

import android.content.Context
import android.text.InputFilter
import android.text.Spanned
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import java.util.regex.Pattern

/**
 * 描述：限制输入空格和减号
 * 作者：ZhangPing
 * 创建时间：2023/4/13 11:45
 */
class SmartEditText(context: Context, attrs: AttributeSet) : AppCompatEditText(context, attrs){

    companion object{
        val spaceFilter = object : InputFilter{
            override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
                if (source.toString() == " " || source.toString() == "-") {
                    return ""
                }
                return source
            }
        }
        val emojiFilter = object : InputFilter{
            override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
                val regex = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE or Pattern.CASE_INSENSITIVE)
                if (regex.matcher(source).find()){
                    return ""
                }
                return source
            }
        }
    }

    init {
        filters = filters.toMutableList().apply {
            add(spaceFilter)
        }.toTypedArray()
    }

}